import { isMatch } from "date-fns";
export const isExternal = path => {
  return /^(https?:|mailto:|tel:)/.test(path);
};

export const isString = str => {
  if (typeof str === "string" || str instanceof String) {
    return true;
  }
  return false;
};

export const isArray = arg => {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
};

export const isPhone = str => {
  return /^1[3|5|7|8|][0-9]{9}$/.test(String(str));
};

export const isEmail = str => {
  return /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(
    String(str)
  );
};

export const isIP = str => {
  return /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(
    String(str)
  );
};

export const isID = str => {
  return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(String(str));
};
/**
 * @name 强密码 包含字母和数字，并且区分大小写，包含字符
 * @param {*} value 传过来的值
 * @returns
 */
export const requirementRegexp = value => {
  return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[-_!@#%^&*()+=])[a-zA-Z\d-_!@#%^&*()+=]{8,}$/.test(
    value
  );
};

export const isDate = (datestr, format) => {
  if (!format) {
    format = "yyyy-MM-dd HH:mm:ss";
  }
  return isMatch(datestr, format);
};
